package com.gitee.aurora.dao.impl;

import com.alibaba.fastjson.JSON;
import com.gitee.aurora.comm.AuroraJdbcTemplate;
import com.gitee.aurora.dao.SjzxCheckDao;
import com.gitee.aurora.entity.TabAjAqBaqsyqkdjb;

import com.gitee.aurora.utils.ResultVO;
import com.gitee.aurora.utils.ResultVOUtils;
import com.gitee.aurora.vo.DataCheckVo;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


import java.util.ArrayList;

import java.util.List;



/**
 * author: jiangfuxiao
 * Date:2022/9/2 11:47
 */
@Repository("sjzxCheckDaoImpl")
public class SjzxCheckDaoImpl implements SjzxCheckDao {

    @Autowired
    private AuroraJdbcTemplate auroraJdbcTemplate;




    @Override
    public List<TabAjAqBaqsyqkdjb> queryTableData(DataCheckVo vo ) {

        StringBuilder sql = new StringBuilder();
        List<TabAjAqBaqsyqkdjb> list = new ArrayList<>();
        sql.append(" SELECT * FROM");
        sql.append(vo.getTableName());
        sql.append("  WHERE 1=1 ");
        if (!StringUtils.isBlank(vo.getCode())){
            sql.append("  AND BADWBH IN (  ").append(vo.getCode()).append(" ) ");
        }
        if (!StringUtils.isBlank(vo.getStartTime())){
            sql.append("  AND DT_UPDATE_TIME >=  ").append(vo.getStartTime());
        }
        if (!StringUtils.isBlank(vo.getEndTime())){
            sql.append("  AND DT_UPDATE_TIME <=  ").append(vo.getEndTime());
        }
        try {
            list = auroraJdbcTemplate.queryForMapToUpperCaseList(sql.toString(), TabAjAqBaqsyqkdjb.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public ResultVO insertData(List list,DataCheckVo vo)  {
        try {
            return auroraJdbcTemplate.insertOrUpdate(JSON.toJSONString(list),vo.getTableName());
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtils.error("批量插入执行错误");
        }
    }

}
